package com.gzyj.server.factory.jtt808.down;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gzyj.data.common.VehicleIncrement;
import com.gzyj.jtt808.v2013.HeaderMsgBodyProperty;
import com.gzyj.jtt808.v2013.Package;
import com.gzyj.jtt808.v2013.protocol.jtt1078.JTT808_0x9206;
import com.gzyj.server.config.LogConfig;
import com.gzyj.server.factory.CommonHandler;
import com.gzyj.server.factory.jtt808.NetWorkSerrvice;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandlerContext;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class JTT808_0x9206_Handler implements CommonHandler {

    @Value("${ftp.server.ip}")
    private String ftpServerIp;

    @Value("${ftp.server.port}")
    private Integer ftpServerPort;

    @Value("${ftp.server.username}")
    private String ftpServerUsername;
    @Value("${ftp.server.password}")
    private String ftpServerPassword;
    @Value("${ftp.server.savePath}")
    private String ftpServersavePath;

    @Override
    public void handler(ChannelHandlerContext ctx, Package msg) throws Exception {

    }

    @Override
    public void handler(ChannelHandlerContext ctx, com.gzyj.jtt808.v2019.Package msg) throws Exception {

    }

    @Override
    public void handler(ChannelHandlerContext ctx, com.gzyj.jtt808.v2011.Package msg) {

    }

    @Autowired
    private NetWorkSerrvice netWorkSerrvice;


    @Override
    public void handler(VehicleIncrement vehicleInfo, String msg) {
        JSONObject command = JSON.parseObject(msg);
        String simNo = command.getString("ipAddress");

        Byte logicChannelNo = command.getByte("logicChannelNo");

        Date beginTime = command.getDate("beginTime");
        Date endTime = command.getDate("endTime");
        Long alarmFlag = command.getLong("alarmFlag");
        Byte aVResourceType = command.getByte("aVResourceType");
        Byte streamType = command.getByte("streamType");
        Byte memoryType = command.getByte("memoryType");

        Integer seq = command.getIntValue("seq");

        String filePath = command.getString("storagePath");


        if (StringUtils.isNotEmpty(vehicleInfo.getTerminalprotocoltype()) && vehicleInfo.getTerminalprotocoltype().equalsIgnoreCase("2019")) {


        } else {

            JTT808_0x9206 jtt8080x9206 = new JTT808_0x9206();

            HeaderMsgBodyProperty headerMsgBodyProperty = new HeaderMsgBodyProperty();
            headerMsgBodyProperty.setSubcontract(false);
            headerMsgBodyProperty.setEncryptMethod(com.gzyj.jtt808.v2013.HeaderEncryptMethod.None);

            jtt8080x9206.getPackageHeader().setMsgBodyProperty(headerMsgBodyProperty);
            jtt8080x9206.getPackageHeader().setTerminalPhoneNo(simNo);
            jtt8080x9206.getPackageHeader().setMsgNum(seq);

            jtt8080x9206.setServerIp(ftpServerIp);
            jtt8080x9206.setPort(ftpServerPort);
            jtt8080x9206.setUserName(ftpServerUsername);
            jtt8080x9206.setPassword(ftpServerPassword);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");

            String path = File.separator + simNo + File.separator + 0x9206 + File.separator + logicChannelNo
                    + File.separator + sdf.format(beginTime) + File.separator + sdf.format(endTime);
            try {
                if (StringUtils.isNotEmpty(filePath))
                    Files.delete(Paths.get(ftpServersavePath + filePath));
            } catch (Exception e) {
                e.printStackTrace();
            }


            jtt8080x9206.setFileUploadPath(path);
            jtt8080x9206.setChannelNo(logicChannelNo);
            jtt8080x9206.setBeginTime(beginTime);
            jtt8080x9206.setEndTime(endTime);
            jtt8080x9206.setAlarmFlag(alarmFlag);
            jtt8080x9206.setMediaType(aVResourceType);
            jtt8080x9206.setStreamType(streamType);
            jtt8080x9206.setMemoryType(memoryType);
            jtt8080x9206.setTaskExcuteCondition((byte) 7);

            netWorkSerrvice.send2Veh(jtt8080x9206.getPackageHeader().getTerminalPhoneNo(), jtt8080x9206.encode());

            LogConfig.commondLog.info("808 2013版 9206 下发 文件上传指令 指令 {} ", simNo);
        }


    }
}
